package czh.cocoa.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.Window;

import czh.cocoa.foundation.NSNotificationCenter;
import static czh.cocoa.ui.UIApplication.*;

public class CocoaActivity extends Activity {

	private UIApplication mApp;
	
	private boolean mFinishLaunched;
			
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		CocoaView view = new CocoaView(this);
		setContentView(view);
	
		mApp = UIApplication.sharedApplication();
		mApp.setRootView(view);
		mApp.finishLaunchingWithOptions();  
	}
	
	public void onResume() {
		super.onResume();
		if (mFinishLaunched) {
			mApp.willEnterForeground();
		} else {
			mFinishLaunched = true;
		}
	}
	
//	public void onWindowFocusChanged(boolean hasFocus) {
//		if (hasFocus) {
//			CocoaView root = (CocoaView) mApp.getRootView(); 
//			root.resumeSoftInputIfNeed();
//		}
//	}
	
	public void onPause() {
		super.onPause();
		mApp.didEnterBackground();
	}
	
	public void onDestroy() {
		super.onDestroy();
		mApp.willTerminate();
	}
	
	public void onBackPressed() {
		moveTaskToBack(false);
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		mApp.receiveMenuCommand(item.getItemId(), item.getTitle().toString());
		return true;
	}
	
	public void onLowMemory() {
		NSNotificationCenter center = NSNotificationCenter.defaultCenter();
		center.postNotificationNameObject(UIApplicationDidReceiveMemoryWarningNotification, null);
	}
	
}
